﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Schedule.UI.ScheduleService;
using Schedule.UI.Foundation;
using System.Windows.Input;

namespace Schedule.UI.ViewModel
{
    public class TeachableTimeViewModel : INotifyPropertyChanged
    {
        public TeachableTimeViewModel()
        {
            RefreshThoigianGiangday();
            this.PropertyChanged += (s, e) => {
                if (insertCurThoigianGiangday != null)
                    insertCurThoigianGiangday.RaiseCanExecuteChanged();
                if (saveCurThoigianGiangday != null)
                    saveCurThoigianGiangday.RaiseCanExecuteChanged();
                if (delCurThoigianGiangday != null)
                    delCurThoigianGiangday.RaiseCanExecuteChanged();
            };
            
        }
        #region Khai bao
        public ScheduleServiceClient ServiceClient = new ScheduleServiceClient();
        #region propchange
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        private IEnumerable<THOIGIANGIANGDAY> thoigianGiangdays;
        public IEnumerable<THOIGIANGIANGDAY> ThoigianGiangdays
        {
            get { return this.thoigianGiangdays; }
            set
            {
                this.thoigianGiangdays = value;
                OnPropertyChange("ThoigianGiangdays");
            }
        }


        private THOIGIANGIANGDAY currentThoigianGiangday;
        public THOIGIANGIANGDAY CurrentThoigianGiangday
        {
            get { return this.currentThoigianGiangday; }
            set
            {
                this.currentThoigianGiangday = value;
                OnPropertyChange("CurrentThoigianGiangday");
            }
        }
        private IEnumerable<GIANGVIEN> giangviens;
        public IEnumerable<GIANGVIEN> Giangviens
        {
            get { return this.giangviens; }
            set
            {
                this.giangviens = value;
                OnPropertyChange("Giangviens");
            }
        }

        private GIANGVIEN currentGiangvien = new GIANGVIEN();
        public GIANGVIEN CurrentGiangvien 
        {
            get { return this.currentGiangvien; }
            set
            {
                this.currentGiangvien = value;
                OnPropertyChange("CurrentGiangvien");
            }
        }
       
        #endregion

        #region refreshPhong
        private void RefreshThoigianGiangday()
        {
            this.ServiceClient.getAllThoigianGiangdayCompleted += (s, e) =>
            {
                this.ThoigianGiangdays = e.Result;
            };
            this.ServiceClient.getAllThoigianGiangdayAsync(currentGiangvien.GiangVienID);
        }

        private void RefreshGiangvien()
        {
            //throw new NotImplementedException();
            this.ServiceClient.getAllGiangvienCompleted += (s, e) =>
            {
                this.giangviens = e.Result;
            };
            this.ServiceClient.getAllGiangvienAsync();
            

        }
        #endregion
        
        #region Save
        private RelayCommand saveCurThoigianGiangday;
        public ICommand SaveCurThoigianGiangday
        {
            get { return saveCurThoigianGiangday ?? (saveCurThoigianGiangday = new RelayCommand(() => SaveThoigianGiangday(), () => this.currentThoigianGiangday != null && this.currentThoigianGiangday.ID != 0)); }
        }

        private void SaveThoigianGiangday()
        {
            ServiceClient.saveThoigianGiangdayAsync(currentThoigianGiangday);
            RefreshThoigianGiangday();
        }

        
        #endregion

        #region del
        private RelayCommand delCurThoigianGiangday;
        public ICommand DelCurThoigianGiangday
        {
            get { return delCurThoigianGiangday ?? (delCurThoigianGiangday = new RelayCommand(() => DelThoigianGiangday(), () => this.currentThoigianGiangday != null && this.currentThoigianGiangday.ID != 0)); }
        }

        private void DelThoigianGiangday()
        {
            ServiceClient.deleteThoigianGiangdayAsync(currentThoigianGiangday);
            RefreshThoigianGiangday();
        }
        #endregion

        #region Insert

        private RelayCommand insertCurThoigianGiangday;
        public ICommand InsertCurThoigianGiangday
        {
            get { return insertCurThoigianGiangday ?? (insertCurThoigianGiangday = new RelayCommand(() => InsertThoigianGiangday(), () => this.currentThoigianGiangday != null)); }
        }

        private void InsertThoigianGiangday()
        {
            ServiceClient.insertThoigianGiangdayAsync(CurrentThoigianGiangday);
            RefreshThoigianGiangday();
        }
        #endregion
        
    }
}
